// @ts-ignore
/* eslint-disable */
import {request} from 'umi';
import {TroubleListItem} from './data';

export async function loadTrouble(params: {
  // query
  /** 当前的页码 */
  current?: number;
  /** 页面的容量 */
  pageSize?: number;
}, sorter: any) {
  return request<{
    data: TroubleListItem[];
    /** 列表的内容总数 */
    total?: number;
    success?: boolean;
  }>('/v2/enterprise/trouble/list', {
    method: 'GET',
    params: {
      ...params,
      pageNum: params.current,
      // orderByColumn: "troubleSort",
      orderByColumn: Object.keys(sorter)[0],
      isAsc: Object.values(sorter)[0],
      type: 0,
    },
  });
}

export async function loadCheckTrouble(params: {
  // query
  /** 当前的页码 */
  current?: number;
  /** 页面的容量 */
  pageSize?: number;
}, sorter: any) {
  return request<{
    data: TroubleListItem[];
    /** 列表的内容总数 */
    total?: number;
    success?: boolean;
  }>('/v2/enterprise/trouble/list', {
    method: 'GET',
    params: {
      ...params,
      pageNum: params.current,
      // orderByColumn: "riskSort",
      // isAsc: 'desc',
      orderByColumn: Object.keys(sorter)[0],
      isAsc: Object.values(sorter)[0],
      type: 1,
    },
  });
}

export async function download() {
  request(`/v2/enterprise/trouble/template`, {
    method: 'POST',
    // 必须加responseType: 'blob',
    responseType: 'blob',
  }).then((res) => {
    const objectURL = URL.createObjectURL(res);
    let btn = document.createElement('a');
    btn.download = '隐患导入模板.xlsx'; //文件类型
    btn.href = objectURL;
    btn.click();
    URL.revokeObjectURL(objectURL);
    // @ts-ignore
    btn = null;
  });
}

export async function checkFirst(param: TroubleListItem): Promise<{ data: number }> {
  return request('/v2/enterprise/trouble/first', {method: 'GET',});
}
export async function doFirst(param: TroubleListItem): Promise<{ data: number }> {
  return request('/v2/enterprise/trouble/first', {method: 'POST',});
}

export async function distribute(param: TroubleListItem): Promise<{ data: number }> {
  return request('/v2/enterprise/trouble/distribute', {method: 'POST',});
}
export async function initTrouble(param: TroubleListItem): Promise<{ data: number }> {
  return request('/v2/enterprise/trouble/init', {method: 'POST',});
}
export async function getTrouble(param: {
  troubleId?: number;
}): Promise<{ data: TroubleListItem }> {
  console.log(param);
  return request(`/v2/enterprise/trouble/${param.troubleId}`, {method: 'GET'});
}

export async function addTrouble(param: TroubleListItem): Promise<{ data: number }> {
  return request('/v2/enterprise/trouble', {method: 'POST', data: param});
}

export async function updateTrouble(param: TroubleListItem): Promise<{ data: number }> {
  return request('/v2/enterprise/trouble', {method: 'PUT', data: param});
}

export async function checkTrouble(param: TroubleListItem): Promise<{ data: number }> {
  return request('/v2/enterprise/trouble/check', {method: 'PUT', data: param});
}

export async function rejectTrouble(param: TroubleListItem): Promise<{ data: number }> {
  return request('/v2/enterprise/trouble/reject', {method: 'PUT', data: param});
}

export async function removeTrouble(data: number[]) {
  return request<Record<string, any>>(`/v2/enterprise/trouble/` + data.join(), {
    method: 'DELETE',
  });
}
